home *** CD-ROM | disk | FTP | other *** search
- //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
- //
- // HSV Demo 1.c
- //
- // Demonstrates drawing with HSV colors. Uses a default System
- // Palette (note the banding which occurs due to default color
- // distribution.)
- //
- // History:
- //
- // 950225 jb: Written
- //
- //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
-
-
- // __#Defines________________________________________________________________________
- #define kMainWindowResID 1000
- #define kAppColorTableResID 72 //ID of nice, 256-color palette provided by System
- #define kSleepTicks 0xFFFFFFFF //relinquish all time; don't want null events
-
- // __#Headers________________________________________________________________________
- #include <Palettes.h>
- #include <math.h>
- #include "Utils.h" //for EnviroCheck
-
- // __#Protos_________________________________________________________________________
- // __ Macros_________________________________________________________________________
- // __ Enums__________________________________________________________________________
- // __ Typedefs_______________________________________________________________________
- // __ Static Protos__________________________________________________________________
- static void ToolBoxInit(void);
- static Boolean OpenMainWindow( void );
- static void ShowHSVCircle( void );
- static void WaitForQuit( void );
-
- // __ Extern Globals_________________________________________________________________
- // __ Static Globals_________________________________________________________________
- static WindowPtr gMainWindow;
-
- // __ Functions______________________________________________________________________
-
-
-
-
-
- //____ main __________________________________________________________________________
- //
- void main( void )
- {
-
- ToolBoxInit();
-
- if (!EnviroCheck())
- ExitToShell();
-
- if (!OpenMainWindow())
- ExitToShell();
-
- ShowHSVCircle();
-
- WaitForQuit();
-
- if(NULL != gMainWindow)
- DisposeWindow(gMainWindow);
-
- }//main
-
-
-
- //____ ToolBoxInit __________________________________________________________________________
- //
- // Basic initialization.
- //
- static void ToolBoxInit( void )
- {
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
- InitCursor();
- FlushEvents(everyEvent, 0);
- } //ToolBoxInit
-
-
- //____ OpenMainWindow __________________________________________________________________________
- //
- // Open our window, install palette built from a resource CTable.
- // provided by the System.
- //
- // All entries in palette marked pmTolerant, with a tolerance of 0x5000.
- //
- static Boolean OpenMainWindow( void )
- {
- PaletteHandle appPalette; // the palette that we will make the window's palette
- CTabHandle tempColorTable;
-
-
- // create and show the window, make sure it's frontmost
- gMainWindow = GetNewCWindow( kMainWindowResID, ( Ptr )NULL, ( WindowPtr ) -1 );
- if (NULL == gMainWindow) //real apps should handle this gracefully
- return FALSE;
- SetWTitle(gMainWindow, "\pHSV Demo 1");
- ShowWindow( gMainWindow );
- SetPort( gMainWindow );
-
- //load in the color table, translate to palette, install in window
- tempColorTable = GetCTable( kAppColorTableResID );
- if (NULL == tempColorTable)
- return FALSE;
-
- appPalette = NewPalette( 256, tempColorTable, pmTolerant, 0x5000 );
- NSetPalette( gMainWindow, appPalette, pmFgUpdates );
-
- DisposeCTable(tempColorTable);
-
- return TRUE;
- }//OpenMainWindow
-
-
- //____ ShowHSVCircle __________________________________________________________________________
- //
- // Walk incrementally around the perimeter of the HSV color wheel, creating HSV colors
- // on each step, translating them to RGB, and then drawing a dot with that color
- // on a circle corresponding the color's origin on the HSV wheel.
- //
- // Changing the value of kNumCircles to a number greater than one will cause more HSV
- // circles to be drawn, each of which will exhibit increasing Saturation and Value.
- //
- static void ShowHSVCircle( void )
- {
- #define kNumSteps 256 //defines how many dots
- #define mThetaStepSize (6.283 / kNumSteps) //how far around our circle to go
- #define mColorStepSize (65536 / kNumSteps) //how far to go around color wheel per dot
- #define kNumCircles 1 // <== try changing this to 5 or 10
- #define kRadius 120 //max radius of our circle
- #define mRadiusStep (kRadius / kNumCircles) //how much to enlarge each circle
- #define mSatValStep (65535 / kNumCircles) //amount to increments saturation and value
- #define kHalfRectSize 20 //halve length to make centering easy
-
- short step;
- short x, y, cenX, cenY;
- double theta;
- Rect aRect;
- short theRadius;
- short circleNum;
- HSVColor ourHSV;
- RGBColor resultingRGB;
-
- cenX = qd.thePort->portRect.right / 2;
- cenY = qd.thePort->portRect.bottom / 2;
-
- resultingRGB.red = resultingRGB.blue = resultingRGB.green = 32768;
- RGBForeColor(&resultingRGB);
- PaintRect(&qd.thePort->portRect);
-
- //start out with full red
- ourHSV.hue = 0;
- ourHSV.saturation = 0;
- ourHSV.value = 0;
- theRadius = 0;
- for (circleNum = 0; circleNum < kNumCircles; circleNum++)
- {
- theRadius += mRadiusStep;
- ourHSV.saturation += mSatValStep;
- ourHSV.value += mSatValStep;
-
- //draw HSV circle
- for (step = 0, theta = 0.0; step < kNumSteps; step++)
- {
- theta += mThetaStepSize;
- x = cenX + theRadius * cos(theta);
- y = cenY - theRadius * sin(theta);
-
- ourHSV.hue += mColorStepSize; //more clockwise along color wheel
- HSV2RGB(&ourHSV,&resultingRGB);
-
- RGBForeColor(&resultingRGB);
-
- SetRect(&aRect, x - kHalfRectSize, y - kHalfRectSize, x + kHalfRectSize, y + kHalfRectSize);
- PaintOval(&aRect);
- }
-
- }//for circleNum
- }//ShowHSVCircle
-
-
- //____ WaitForQuit __________________________________________________________________________
- //
- // WaitForQuit -
- //
- // Hangs out until there is any keyboard or mouse activity. Not a very useful event loop,
- // but sufficient for our demo.
- //
- static void WaitForQuit( void )
- {
- Boolean wait;
- EventRecord theEvent;
-
- wait = TRUE;
- while (wait)
- {
- if(WaitNextEvent(everyEvent, &theEvent, kSleepTicks, 0L))
- {
- switch (theEvent.what)
- {
- case mouseDown:
- case keyDown:
- wait = FALSE;
- break;
- }//switch (theEvent.what)
- }//if WaitNextEvent
- }//while
-
- }//WaitForQuit